
var modelImgUrl="../../../images/";
var windowSize = require("../../../js/getWindowSize.js");
var windowWidth=windowSize.getWindowSize().windowWidth;
var windowHeight = windowSize.getWindowSize().windowHeight;
const requestMode = require('../../../js/requestsMode.js');
const constants = require('../../../js/constants.js');
const session = require('../../../js/session.js');
const  Tools = require('../../../js/Tools.js');
Page({
    data:{
        orderType:[
            {id:0,name:'全部'},
            {id:1,name:'待支付'},
            {id:2,name:'待发货'},
            {id:3,name:'待收货'},
            {id:4,name:'已完成'},
            {id:5,name:'退单'}
        ],
        activeTypeId:0,
        windowWidth:windowWidth,
        slideHasWidth:windowWidth*6,
        slideWidth:0,
        allType:[],
        contentMarginTop:60,
        scrollTop0:0,
        scrollTop1:0,
        scrollTop2:0,
        scrollTop3:0,
        scrollTop4:0,
        scrollTop5:0,
        contentPaddingBottom:0,
        src:modelImgUrl,
        animationTime:"transition: all 0s;",
        page : 1,
        deleteId:'',
        notOrder:'../../../images/notOrder.png',
        showNotOrder : true
    },
    onLoad:function(res){
        let This =this;
        let getType = res.type;
        let wantSeeOrderType = '';
        if(getType == 'FALSE'){
            This.setData({
                slideWidth : 0
            });
        }else{
            wantSeeOrderType =parseInt( getType );
            This.setData({
                activeTypeId:wantSeeOrderType+1,
                slideWidth:-(wantSeeOrderType+1)*windowWidth
            });
        }
        let query = wx.createSelectorQuery();
        query.select('#topIsOrderType').boundingClientRect();
        query.exec(function (res) {
            This.setData({
                contentPaddingTop:(res[0].height)+5,
                contentPaddingBottom:(res[0].height)+20,
                contentHeight : windowHeight - ((res[0].height)+5)
            })
        });
        This.queryOrderList(wantSeeOrderType,1);
    },
    queryOrderList : function (status,page) {
        Tools.showLoaddingToast("加载中...");
        let that = this;
        that.setData({
            nowStatus : status,
            deleteId : ''
        });
        requestMode.sendRequest('order/list','POST',{status:status,page:page,size:constants.size},{sKey:session.get('sKey')}).then(function(querySuccess){
            let code = querySuccess.data.code;
            if(code == 200){
                Tools.hideLoaddingToast();
                let queryOrderList = querySuccess.data.data.list;
                that.setData({
                    page_total : querySuccess.data.data.page_total
                });
                let allType = that.data.allType;
               queryOrderList.forEach(function(v){
                   v['typeId'] = v['status'];
                   v['orderId'] = v['order_number'];
                   v['orderCount'] = v['goods_info']['total'];
                   v['orderArr'] = v['goods_info']['list'];
                   let vStatus = v['status'];
                   v['orderStatus'] = vStatus;
                   if(vStatus == 0){
                       v['status'] = '未支付'
                   }
                   if(vStatus == 1){
                       v['status'] = '待发货'
                   }
                   if(vStatus == 2){
                       v['status'] = '待收货'
                   }
                   if(vStatus == 3){
                       v['status'] = '已完成'
                   }
                   if(vStatus == 4){
                       v['status'] = '已撤销订单'
                   }
                   if(vStatus == 5){
                       v['status'] = '退单处理中'
                   }
                   if(vStatus == 6){
                       v['status'] = '商家拒绝退单'
                   }
                   if(vStatus == 7){
                       v['status'] = '商家同意退单'
                   }
                   allType.push(v);
               });
                that.setData({
                    showNotOrder : false
                });
                that.setData({
                    allType : allType
                });
                Tools.hideLoaddingToast();
            }else{
                Tools.hideLoaddingToast();
                Tools.showErrorToast(querySuccess.data.message);
                that.setData({
                    showNotOrder : true
                })
            }
        },function(fail){
            Tools.hideLoaddingToast();
            Tools.showWrongNetWorkToast();
        })
    },
    choseType:function(e){
        let typeId = '';
        if(e.formNotGetId){
            typeId = e.formNotGetId
        }else{
            typeId =  e.currentTarget.dataset.id;
        }
        let This = this;
        let activeTypeId = This.data.activeTypeId;
        if(typeId != activeTypeId){
            This.setData({
                animationTime:"transition: all 0.3s;",
                activeTypeId:typeId,
                slideWidth:-parseInt(typeId)*windowWidth,
                allType : [],
                page : 1,
                page_total : 0
            });
            let getOrderStatus = 0;
            if(typeId == 0){
                getOrderStatus = false;
            }else{
                getOrderStatus = typeId-1;
            }
            This.queryOrderList(getOrderStatus,1);
            let newArr = {};
            setTimeout(function(){
                for(let current = 0;current <= 5;current++){
                    if(current != typeId){
                        let name = "scrollTop"+current;
                        let newKey = name;
                        newArr[newKey] = 0;
                        This.setData(newArr)
                    }
                }
            },300);
        }
    },
    toBottom: function () {
        if (this.data.page >= this.data.page_total) {
            return;
        }
        let This = this;
        this.setData({
            page:This.data.page+1
        });
        this.queryOrderList(This.data.nowStatus,This.data.page);
    },
    //待支付---取消订单
    removeOrder:function(e){
        let orderId = e.currentTarget.dataset.id;
        let currentIndex = e.currentTarget.dataset.index;
        let that  = this;
        wx.showModal({
            content: '您确定取消此订单吗？',
            success: function (res) {
                if (res.confirm) {
                    Tools.showLoaddingDefault();
                    requestMode.sendRequest('order/remove','POST',{order_id:orderId},{sKey:session.get('sKey')}).then(function (removeOrderSuccess) {
                        let getCode = removeOrderSuccess.data.code;
                        if(getCode == 200){
                            Tools.showSuccessInfoToast('操作成功');
                            Tools.hideLoaddingToast();
                            that.deleteStyleFunction(orderId,currentIndex)
                        }else{
                            Tools.showErrorToast(removeOrderSuccess.data.message);
                        }
                    },function(){
                        Tools.showWrongNetWorkToast();
                    })
                }
            }
        });
    },
    removeBackOrder: function (e) {
        let orderId = e.currentTarget.dataset.id;
        let currentIndex = e.currentTarget.dataset.index;
        let orderStatus =e.currentTarget.dataset.orderStatus;
        let that  = this;
        if(orderStatus!=7){
            Tools.showErrorToast('退单中不可删除！')
        }else{
            wx.showModal({
                content: '您确定取消此订单吗？',
                success: function (res) {
                    if (res.confirm) {
                        Tools.showLoaddingDefault();
                        requestMode.sendRequest('order/remove','POST',{order_id:orderId},{sKey:session.get('sKey')}).then(function (removeOrderSuccess) {
                            let getCode = removeOrderSuccess.data.code;
                            if(getCode == 200){
                                Tools.showSuccessInfoToast('操作成功');
                                Tools.hideLoaddingToast();
                                that.deleteStyleFunction(orderId,currentIndex)
                            }else{
                                Tools.showErrorToast(removeOrderSuccess.data.message);
                            }
                        },function(){
                            Tools.showWrongNetWorkToast();
                        })
                    }
                }
            });
        }
    },
    goPay: function (e) {
        let orderId = e.currentTarget.dataset.id;
        wx.navigateTo({
            url:'../orderDetailed_payByweiChart/orderDetailed_payByweiChart?order_id='+orderId
        })
    },
    //已完成---删除订单
    deleteHasOverOrder:function(e){
        let orderId = e.currentTarget.dataset.id;
        let currentIndex = e.currentTarget.dataset.index;
        let that  = this;
        wx.showModal({
            content: '您确定删除此订单吗？',
            success: function (res) {
                if (res.confirm) {
                    Tools.showLoaddingDefault();
                    requestMode.sendRequest('order/remove','POST',{order_id:orderId},{sKey:session.get('sKey')}).then(function (removeOrderSuccess) {
                        let getCode = removeOrderSuccess.data.code;
                        if(getCode == 200){
                            Tools.showSuccessInfoToast('操作成功');
                            Tools.hideLoaddingToast();
                            that.deleteStyleFunction(orderId,currentIndex)
                        }else{
                            Tools.showErrorToast(removeOrderSuccess.data.message);
                        }
                    },function(){
                        Tools.showWrongNetWorkToast();
                    })
                }
            }
        });
    },
    //代发货 -- 撤销订单
    revokeOrder : function(e){
        let that = this;
        let orderId = e.currentTarget.dataset.id;
        let orderIndex = e.currentTarget.dataset.index;
        wx.showModal({
            content: '您确定撤销订单吗？',
            success: function (res) {
                if (res.confirm) {
                    Tools.showLoaddingDefault();
                    requestMode.sendRequest('order/revoke','POST',{order_id:orderId},{sKey:session.get('sKey')}).then(function (revokeSuccess) {
                        let code = revokeSuccess.data.code;
                        Tools.hideLoaddingToast();
                        if(code == 200){
                            Tools.showSuccessInfoToast('操作成功');
                            that.deleteStyleFunction(orderId,orderIndex)
                        }else{
                            Tools.showErrorToast(revokeSuccess.data.message);
                        }
                    },function(revokeFail){
                        Tools.hideLoaddingToast();
                        Tools.showWrongNetWorkToast();
                    })
                }

            }
        });
    },
    //待收货 --- 申请退单
    rejectedOrder: function (e) {
        let that = this;
        let orderId = e.currentTarget.dataset.id;
        let orderIndex = e.currentTarget.dataset.index;
        wx.showModal({
            content: '您确定申请退单吗？',
            success: function (res) {
                if (res.confirm) {
                    wx.navigateTo({
                        url: '../applyReturnGoods/applyReturnGoods?orderId='+orderId
                    })
                }
            }
        });
    },
    //已完成 --- 确认收货
    confirmGetGoods: function (e) {
        let orderId = e.currentTarget.dataset.id;
        let that  = this;
        wx.showModal({
            content: '您确定已收到商品？',
            success: function (res) {
                if (res.confirm) {
                    Tools.showLoaddingDefault();
                    requestMode.sendRequest('order/receipt','POST',{order_id:orderId},{sKey:session.get('sKey')}).then(function (removeOrderSuccess) {
                        let getCode = removeOrderSuccess.data.code;
                        if(getCode == 200){
                            Tools.showSuccessInfoToast('操作成功');
                            Tools.hideLoaddingToast();
                            //that.deleteStyleFunction(orderId,currentIndex)
                            let data = {
                                formNotGetId : 4
                            };
                            that.choseType(data);
                        }else{
                            Tools.showErrorToast(removeOrderSuccess.data.message);
                        }
                    },function(){
                        Tools.showWrongNetWorkToast();
                    })
                }
            }
        });
    },
    //模板删除样式
    deleteStyleFunction:function(orderId,orderIndex){
        let that = this;
        that.setData({
            deleteId : orderId
        });
        setTimeout(function(){
            let allType = that.data.allType;
            allType.splice(orderIndex,1);
            that.setData({
                allType : allType,
                deleteId : ''
            });
            if(allType.length == 0){
                that.setData({
                    showNotOrder : true
                })
            }else{
                that.setData({
                    showNotOrder : false
                })
            }
        },300)
    }
});